package com.suning.tigbs.impl;

import com.suning.tigbs.Enum.SequenceNumberEnum;
import com.suning.tigbs.bean.SequenceNumber;
import com.suning.tigbs.dao.SequenceNumberDao;
import com.suning.tigbs.service.SequenceNumberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SequenceNumberServiceImpl implements SequenceNumberService {

    @Resource
    private SequenceNumberDao sequenceNumberDao;

    @Override
    public synchronized String newSequenceNumberEnum(SequenceNumberEnum sequenceNumberEnum) {
        if (sequenceNumberEnum == null) {
            return null;
        }
        SequenceNumber sequenceNumber = sequenceNumberDao.newSequenceNumber(sequenceNumberEnum.getSequenceNumber().getPrefix());

        if (sequenceNumber == null) {
            sequenceNumber = sequenceNumberEnum.getSequenceNumber();
            sequenceNumber.setToday(sequenceNumberDao.getToday());
        }

        sequenceNumberDao.updateSequenceNumber(sequenceNumber);
        return String.format("%s%6s%08d", sequenceNumber.getPrefix(), sequenceNumber.getToday(), sequenceNumber.getCurrentNum());
    }
}
